package org.restoran.model;


import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import java.io.Serializable;

/**
 * Embeddaed class that represent composite primari key on {@link Order} and {@link Item}.
 *
 * @author Snisar Bogdan.
 */
@Embeddable
public class ItemOrderPk implements Serializable {

    @ManyToOne
    private Order order;

    @ManyToOne
    private Item item;

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder().append("{itemId=  ").append((item == null) ? "null" : item.getId());
        s.append(", orderId= ").append((order == null) ? "null" : order.getId());
        return s.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ItemOrderPk)) return false;

        ItemOrderPk that = (ItemOrderPk) o;

        if (item != null ? !item.getId().equals(that.item.getId()) : that.item != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return item != null ? item.hashCode() : 0;
    }
}


